import { _decorator, Component, Label, Node, Sprite, Tween, tween, Vec3 } from 'cc';
import { GAME_SPRITEFRAME } from '../../../Data/GlobalData';
import { TweenTools } from '../../../Tool/TweenTools';
const { ccclass, property } = _decorator;

@ccclass('SingleMajiang')
export class SingleMajiang extends Component {

    //麻将索引
    public MajiangIndex: number = 0;

    //麻将
    @property(Sprite)
    private MajiangSprite: Sprite = null;

    //横轴位置
    private SpaceX: number = 0;

    //放大缩小动画
    private BigTween = null;

    protected start(): void {
        this.node.getChildByName("Label").active = false;
        this.SpaceX = this.node.getPosition().x;
        this.BigTween = tween(this.MajiangSprite.node).to(0.3, { scale: new Vec3(1, 1, 1) }).to(0.3, { scale: new Vec3(0.9, 0.9, 0.9) })
    }

    /**
     * 更新麻将
     */
    public UpdateMajiang(index: number) {
        this.node.getChildByName("Label").getComponent(Label).string = this.node.getSiblingIndex().toString();
        this.SpaceX = this.node.getPosition().x;
        this.MajiangIndex = index;
        this.MajiangSprite.spriteFrame = app.resManager.SpriteFrames[GAME_SPRITEFRAME.Majiang + this.MajiangIndex];
    }

    /**
     * 播放移动动画
     */
    public PlayMoveAni(_worldpos: Vec3) {
        // this.MajiangSprite.node.setWorldPosition(_worldpos.subtract(new Vec3(this.SpaceX)));
        this.MajiangSprite.node.setWorldPosition(_worldpos);
        tween(this.MajiangSprite.node).to(0.2, { position: new Vec3(0, 0, 0) }, {
            easing: 'backOut'
        }).start();
    }

    /**
     * 被选择动画 位置向上移动，忽大忽小
     */
    public IamChoose() {
        this.node.setPosition(this.SpaceX, 15);
        tween(this.MajiangSprite.node).repeatForever(this.BigTween).start();

    }

    /**
     * 我放下 位置回复 左右晃动
     */
    public ImDown() {
        Tween.stopAllByTarget(this.MajiangSprite.node);
        tween(this.MajiangSprite.node).to(0.05, { angle: 10 }).to(0.05, { angle: -10 }).to
            (0.05, { angle: 0 }).call(() => {
                this.IamReset();
            }).start();
    }

    /**
     * 我恢复
     */
    public IamReset() {
        this.node.setPosition(this.SpaceX, 0);
        Tween.stopAllByTarget(this.MajiangSprite.node);
        this.MajiangSprite.node.setScale(0.9, 0.9, 0.9);
        this.MajiangSprite.node.angle = 0;
    }

}


